home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11083 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: fsgi01.fnal.gov!not-for-mail
  2. From: b91926@fsgi01.fnal.gov (David Sachs)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating operator= for *pointers* to objects??
  5. Date: 12 Mar 1996 11:04:13 -0600
  6. Organization: FERMILAB, Batavia, IL
  7. Message-ID: <4i4aqd$e2b@fsgi01.fnal.gov>
  8. References: <Do4KtH.MK@mv.mv.com>
  9. Reply-To: sachs@fnal.fnal.gov
  10. NNTP-Posting-Host: fsgi01.fnal.gov
  11. X-Newsreader: NN version 6.5.0 #9 (NOV)
  12.  
  13. bnb@curtech.com (Brian Ballard) writes:
  14.  
  15.  
  16.  
  17. >I'm trying to find a way to create an assignment (or copy) operator
  18. >which I can use to apply to pointers to object.  For example, say I have
  19. >a class hierarchy that looks like:
  20.  
  21. >         |--- B
  22. >    A ---|--- C
  23. >         |--- D
  24.  
  25. >Millions of these objects can potentially be created and scrapped every
  26. >few minutes so, in order to improve performance and reduce memory
  27. >fragmentation, every class in the hierarchy has their new/delete operators
  28. >overloaded to use a handful of "pre-built" objects in the freestore.
  29. >Pretty standard stuff.
  30.  
  31. >Because exact duplicates of an instantiated object can (and must) exist,
  32. >class A has a refCount member which allows me to keep track of how many
  33. >copies of a duplicated object exist out there, so I don't pull the rug
  34. >out from under an object that is still being used.
  35.  
  36. >In general, megabytes worth of network packets are stored in a file, which
  37. >get read in by this software and are wrapped up in one of the above derived
  38. >classes (B, C, or D) depending upon the type of packet.  This creates a
  39. >virtual "list" of sorts where I have an iterator which walks through the
  40. >file, returning objects of type B/C/D.
  41.  
  42. >Basically, I'd like to be able to do this:
  43.  
  44. >{
  45. > A*    start = NULL;
  46. > A*    temp = NULL;
  47.  
  48. > start = mStream->GetPacketAtOffset(0L);    // This might return an object B*
  49. > temp = start;                              // I need this to call my oper=
  50. > while ( temp = mStream->GetNextPacket(temp)) // GetNextPacket automatically
  51. >    bla bla bla                               // 'delete's first A* temp
  52.  
  53. >...
  54. >} 
  55.  
  56. >In the above example, I need "temp = start" to "delete temp", create a
  57. >duplicate of "start" and return its pointer into "temp"
  58.  
  59. >Right now, I have to do this:
  60.  
  61. >{
  62. >...
  63. >   delete temp;
  64. >   temp = start->Copy();
  65. >...
  66. >}
  67.  
  68. >Not a big deal to it this way, but it's easy to forget over the long haul
  69. >that these steps need to be taken.  It's much easier to do a simple pointer
  70. >assignment which guarantees that every pointer to an object is accounted for.
  71.  
  72.  
  73. >OK.... a bit much content for such an easy question....
  74.  
  75.  
  76. Since operators cannot be overloaded for build-in types, you may
  77. wish to define a "smart pointer" class to refer to your objects.
  78.  
  79. You will have to define several overloaded functions in this
  80. class, most notably operator->.
  81.  
  82. One thing you can do is make the smart pointer class a friend of
  83. your base class, and make the constructors and destructors of your
  84. base class private. This will prevent all creation of your
  85. controlled objects, excpet via your opinter class.
  86. -- 
  87. ***** Listen Americans, the IRS is your taxer,  the IRS is one. *****
  88. David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
  89. Voice: 1 708 840 3942      Deparment Fax: 1 708 840 3785
  90.